package org.dromara.system.openApi.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.openApi.domain.bo.OpenApiClientBo;
import org.dromara.system.openApi.domain.vo.OpenApiClientVo;
import org.dromara.system.openApi.domain.OpenApiClient;
import org.dromara.system.openApi.mapper.OpenApiClientMapper;
import org.dromara.system.openApi.service.IOpenApiClientService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * openApiClientService业务层处理
 *
 * @author wdm
 * @date 2024-03-13
 */
@RequiredArgsConstructor
@Service
public class OpenApiClientServiceImpl implements IOpenApiClientService {

    private final OpenApiClientMapper baseMapper;

    /**
     * 查询openApiClient
     */
    @Override
    public OpenApiClientVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    @Override
    public OpenApiClient queryByClientId(String clientId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<OpenApiClient>().eq(OpenApiClient::getClientId, clientId).eq(OpenApiClient::getStatus,"1"));
    }

    /**
     * 查询openApiClient列表
     */
    @Override
    public TableDataInfo<OpenApiClientVo> queryPageList(OpenApiClientBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OpenApiClient> lqw = buildQueryWrapper(bo);
        Page<OpenApiClientVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询openApiClient列表
     */
    @Override
    public List<OpenApiClientVo> queryList(OpenApiClientBo bo) {
        LambdaQueryWrapper<OpenApiClient> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OpenApiClient> buildQueryWrapper(OpenApiClientBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OpenApiClient> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getClientId()), OpenApiClient::getClientId, bo.getClientId());
        lqw.eq(StringUtils.isNotBlank(bo.getClientSecret()), OpenApiClient::getClientSecret, bo.getClientSecret());
        lqw.like(StringUtils.isNotBlank(bo.getClientName()), OpenApiClient::getClientName, bo.getClientName());
        lqw.eq(bo.getIsInner() != null, OpenApiClient::getIsInner, bo.getIsInner());
        lqw.eq(bo.getStatus() != null, OpenApiClient::getStatus, bo.getStatus());
        lqw.eq(bo.getRequireEncrypt() != null, OpenApiClient::getRequireEncrypt, bo.getRequireEncrypt());
        lqw.eq(bo.getCheckSign() != null, OpenApiClient::getCheckSign, bo.getCheckSign());
        lqw.eq(bo.getDiffCurTime() != null, OpenApiClient::getDiffCurTime, bo.getDiffCurTime());
        return lqw;
    }

    /**
     * 新增openApiClient
     */
    @Override
    public Boolean insertByBo(OpenApiClientBo bo) {
        OpenApiClient add = MapstructUtils.convert(bo, OpenApiClient.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改openApiClient
     */
    @Override
    public Boolean updateByBo(OpenApiClientBo bo) {
        OpenApiClient update = MapstructUtils.convert(bo, OpenApiClient.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OpenApiClient entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除openApiClient
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
